﻿using System.Collections.Generic;
using System.Reflection.Metadata;
using AutoMapper;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购到货单显示
    /// </summary>
    public class GetPurchaseArrivalCommdHand : IRequestHandler<GetPurchaseArrivalCommd, APIResult<List<PurchaseArrivalDto>>>
    {
        /// <summary>
        ///采购到货
        /// </summary>
        private readonly IBaseRepository<PurchaseArrival> repository;
        /// <summary>
        /// 物料信息
        /// </summary>
        private readonly IBaseRepository<Materials_Info> baseRepository;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="baseRepository"></param>
        public GetPurchaseArrivalCommdHand(IBaseRepository<PurchaseArrival> repository, IBaseRepository<Materials_Info> baseRepository)
        {
            this.repository = repository;
            this.baseRepository = baseRepository;
        }
         /// <summary>
         /// 处理方法
         /// </summary>
         /// <param name="request"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
        public Task<APIResult<List<PurchaseArrivalDto>>> Handle(GetPurchaseArrivalCommd request, CancellationToken cancellationToken)
        {

            APIResult<List<PurchaseArrivalDto>> result =new APIResult<List<PurchaseArrivalDto>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = from a in repository.GetAll().Result
                       join b in baseRepository.GetAll().Result
                       on a.PurchaseArrivaId equals b.MaterialsInfoid
                       select new PurchaseArrivalDto
                       {
                           PurchaseArrivaId =a.PurchaseArrivaId,
                           DocumentCode =a.DocumentCode,
                           DocumentStatus =a.DocumentStatus,
                           BusinessStatus =a.BusinessStatus,
                           Documentid =a.Documentid,
                           BusinessName =a.BusinessName,
                           PurchasingDe = a.PurchasingDe,
                           PurchasingTiem =a.PurchasingTiem,
                           PurchaseOrder = a.PurchaseOrder,
                           Supplier = a.Supplier,
                           Warehouse = a.Warehouse,
                           ProgressStatus = a.ProgressStatus,
                           RemarksInformation = a.RemarksInformation,
                           MaterialsInfoid = b.MaterialsInfoid,
                           SaleCode = b.SaleCode,
                           Document =b.Document,
                           Materialid =b.Materialid,
                           MaterialName =b.MaterialName,
                           ModelSper =b.ModelSper,
                           ModePrimary = b.ModePrimary,
                           ModeDate = b.ModeDate,
                           ModeQuantity = b.ModeQuantity,
                           ModeUnitPrice = b.ModeUnitPrice,
                           ModeAmount= b.ModeAmount,
                           ModeReceived=b.ModeReceived,
                           ModePurchase =b.ModePurchase,
                       };
            if (!string.IsNullOrEmpty(request.Document))
            {
                list = list.Where(X => X.Document == request.Document);
            }


            if (!string.IsNullOrEmpty(request.DocumentCode))
            {
                list = list.Where(X => X.DocumentCode == request.DocumentCode);
            }
            if (!string.IsNullOrEmpty(request.Supplier))
            {
                list = list.Where(X => X.Supplier == request.Supplier);
            }

            // 添加需求日期范围查询条件
            if (request.ModeDateStart.HasValue && request.ModeDateEnd.HasValue)
            {
                list = list.Where(x => x.DocumentStatus >= request.ModeDateStart && x.ModeDate <= request.ModeDateEnd);
            }
            else if (request.ModeDateStart.HasValue)
            {
                list = list.Where(x => x.DocumentStatus >= request.ModeDateStart);
            }
            else if (request.ModeDateEnd.HasValue)
            {
                list = list.Where(x => x.DocumentStatus <= request.ModeDateEnd);
            }
            result.data =list.ToList();


            return Task.FromResult(result);


        }
    }
}
